<div class="filter">
    <div class="f1 item">
        <select data-am-selected>
            <option value="-1">全部消息</option>
            <option value="0">全员消息</option>
        </select>
    </div>
    <div class="f4 item">
        <select data-am-selected>
            <option value="-1">所有类型</option>
            <option value="首页弹窗">首页弹窗</option>
            <option value="右下弹窗">右下弹窗</option>
            <option value="消息中心">消息中心</option>
        </select>
    </div>
    <div class="f5 item">
        <select data-am-selected>
            <option value="-1">弹窗次数</option>
            <option value="1,1">一次弹窗</option>
            <option value="9999,9999">永久弹窗</option>
        </select>
    </div>
    <div class="f2 item">
        <input type="text" class="am-form-field" placeholder="用户名" />
    </div>
    
    <div class="f3 button item">
        <button type="button" class="am-btn am-btn-primary btn-search">查询</button>
    </div>
    <div class="button item">
        <button type="button" class="am-btn am-btn-primary btn-add">添加消息</button>
    </div>
    <div class="clear"></div>
</div>

<div class="table">
    <table class="am-table am-table-bordered am-table-radius am-table-striped am-table-compact">
        <thead>
            <tr>
                <th width="80px">账号</th>
                <th width="100px">标题</th>
                <th>内容</th>
                <th width="80px">类型</th>
                <th width="60px">显示一次</th>
                <th width="100px">添加人</th>
                <th width="100px">时间</th>
                <th width="60px">操作</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

    <div class="zxf_pagediv"></div>
</div>

<script src="../public/all/js/notice.js"></script>
<script src="../public/all/js/pages/notice_ui.js"></script>
<script>
    // 权限控制
    var m = {
        "/notice/save": "btn-add",
    };
    Util.applyRights(m);

    // 添加消息
    $(".btn-add").click(function() {
        context.center.noticeOper = 'add';
        parent._f = _f;
        NoticeUI.showForm();
    });
    Util.restorePageFilter();

    var _f = Util.queryTemplate($(".table tbody"), ".zxf_pagediv", $(".f3 button"), function(page, size, cb) {
        var self = this;
        var all = $(".f1 select").val();
        var uid = undefined;
        if (all == '0') {
            uid = '';
        }
        else {
            var v = $(".f2 input").val().trim();
            if (v) uid = v;
        }
        var type = $(".f4 select").val();
        if (type == '-1') type = null;

        var count = $(".f5 select").val();
        if (count == -1) count = null;

        Util.savePageFilter([".f1 select", ".f2 input", ".f4 select", ".f5 select"]);
        CMD.notice_query(uid, type, count, size, page, cb);
    }, function(table, list, sum) {
        for (var i in list) {
            var d = list[i]; d = Util.convertNotice(d);
            var tr = $("<tr></tr>");
            tr.append('<td>' + (d.user.id ? d.user.mobile : ' ') + '</td>');
            tr.append('<td>' + d.subject + '</td>');
            tr.append('<td class="detail" value="' + i + '">' + d.content + '</td>');
            tr.append('<td>' + (d.type || '') + '</td>');
            tr.append('<td>' + (d.forever == "1" ? "否" : "是") + '</td>');
            tr.append("<td>" + d.creator.mobile + "</td>");
            tr.append("<td>" + d.time.substring(5) + "</td>");
            if (Rights.check("/notice/delete", context.user.user.grade || '1'))
                tr.append('<td class="oper">\
                    <button class="delete" value="' + i + '">删除</button></td>');
            else
                tr.append('<td> </td>');
            table.append(tr);
        }

        var arr = $(".table td.detail");

        $(".oper button.delete").click(function(e) {
            var index = $(e.currentTarget).attr("value");
            var data = list[index];
            startLoading();
            CMD.notice_delete(data.id, function() {
                stopLoading();
                _f();
            });
        });
        $(".oper button.edit").click(function(e) {
            var index = $(e.currentTarget).attr("value");
            var data = list[index];
            context.center.notice = data;
            context.center.noticeOper = 'edit';
            NoticeUI.showForm();
        });
    });
</script>